package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.service.DocService;
import com.ywjh.inter.service.SportService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Doc extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = -790236092155263113L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		System.out.println(postData);
		String act = postData.getString("act");
		int r = 1;
		String e = "";
		JSONObject json = new JSONObject();
		switch (act) {
		case "disease":
			JSONArray array=DocService.getInstance().getDisease();
			json.put("d", array);
			break;
		case "his_disease":
			JSONArray array1=DocService.getInstance().getHisDisease();
			json.put("d", array1);
			break;
		case "medicine":
			JSONArray array2=DocService.getInstance().getMedicine();
			json.put("d", array2);
			break;
		case "local":
			JSONArray array3=DocService.getInstance().getLocation();
			json.put("d", array3);
			break;
		case "bglu_plan":
			JSONArray array4=DocService.getInstance().getBGluPlan();
			json.put("d", array4);
			break;
		case "group":
			JSONArray array5=DocService.getInstance().getGroup();
			json.put("d", array5);
			break;
		case "group_disease":
			JSONArray array6=DocService.getInstance().getGroupDis();
			json.put("d", array6);
			break;
		case "sport_way":
			JSONArray array7=new JSONArray();
			List<String> list=SportService.getInstance().getWay();
			int i=1;
			for(String way:list) {
				JSONObject data=new JSONObject();
				data.put("id", i);
				data.put("name", way);
				array7.add(data);
				i++;
			}
			json.put("d", array7);
			break;
		case "sport_per":
			JSONArray array8=new JSONArray();
			List<String> list2=SportService.getInstance().getPer();
			i=1;
			for(String per:list2) {
				JSONObject data=new JSONObject();
				data.put("id", i);
				data.put("name", per);
				array8.add(data);
				i++;
			}
			json.put("d", array8);
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
	
}
